package net.zoneland.zrdp.quartz.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import javax.sql.DataSource;
import java.util.Properties;

/**
* 定时任务配置（单机部署建议删除此类和qrtz数据库表，默认走内存会最高效）
*
* @author zonevue
*/
@Configuration(proxyBeanMethods = false)
public class ScheduleConfig {

    @Value("${spring.quartz.properties.org.quartz.jobStore.tablePrefix}")
    private String tablePrefix;

    @Value("${spring.quartz.properties.org.quartz.scheduler.instanceName}")
    private String instanceName;

    @Value("${spring.quartz.properties.org.quartz.scheduler.instanceId}")
    private String instanceId;

    @Value("${spring.quartz.properties.org.quartz.threadPool.class}")
    private String threadPoolClassName;

    @Value("${spring.quartz.properties.org.quartz.threadPool.threadCount}")
    private String threadCount;

    @Value("${spring.quartz.properties.org.quartz.threadPool.threadPriority}")
    private String threadPriority;

    @Value("${spring.quartz.properties.org.quartz.jobStore.class}")
    private String jobStoreClass;

    @Value("${spring.quartz.properties.org.quartz.jobStore.isClustered}")
    private boolean isClustered;

    @Value("${spring.quartz.properties.org.quartz.jobStore.txIsolationLevelSerializable:true}")
    private String txIsolationLevelSerializable;

    @Value("${spring.quartz.properties.org.quartz.jobStore.driverDelegateClass:}")
    private String driverDelegateClass;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(final DataSource dataSource) {

        final SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setDataSource(dataSource);

        // quartz参数
        final Properties prop = new Properties();
        prop.put("org.quartz.scheduler.instanceName", instanceName);
        prop.put("org.quartz.scheduler.instanceId", instanceId);
        // 线程池配置
        prop.put("org.quartz.threadPool.class", threadPoolClassName);
        prop.put("org.quartz.threadPool.threadCount", threadCount);
        prop.put("org.quartz.threadPool.threadPriority", threadPriority);
        // JobStore配置
        prop.put("org.quartz.jobStore.class", jobStoreClass);
        // 集群配置
        prop.put("org.quartz.jobStore.isClustered", isClustered);
        prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
        prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
        if (StringUtils.isNotEmpty(txIsolationLevelSerializable)) {
            prop.put("org.quartz.jobStore.txIsolationLevelSerializable", txIsolationLevelSerializable);
        }
        if (StringUtils.isNotEmpty(driverDelegateClass)) {
            prop.put("org.quartz.jobStore.driverDelegateClass", driverDelegateClass);
        }
        prop.put("org.quartz.jobStore.misfireThreshold", "12000");
        prop.put("org.quartz.jobStore.tablePrefix",tablePrefix);
        factory.setQuartzProperties(prop);

        factory.setSchedulerName("ZonevueScheduler");
        // 延时启动
        factory.setStartupDelay(1);
        factory.setApplicationContextSchedulerContextKey("applicationContextKey");
        // 可选，QuartzScheduler
        // 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
        factory.setOverwriteExistingJobs(true);
        // 设置自动启动，默认为true
        factory.setAutoStartup(true);

        return factory;
    }
}
